package pl.edu.agh.pros.proxy.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import pl.edu.agh.pros.conf.XMPPConfiguration;
import pl.edu.agh.pros.proxy.logic.dao.AppConfigurationDAO;
import pl.edu.agh.pros.proxy.logic.dao.register.RegisteredClientDAO;
import pl.edu.agh.pros.proxy.logic.dao.register.RegisteredTopicDAO;
import pl.edu.agh.pros.proxy.model.XmppServerConfiguration;

@Path("/gae")
public class GAEService {

	@GET
	@Path("/xmpp")
	@Produces("application/json")
	public XMPPConfiguration getXMPPConfiguration(@QueryParam("appKey") String appKey, @QueryParam("devId") String devId) {
		XmppServerConfiguration xmppServerConfiguration = AppConfigurationDAO.getInstance().getXmppServerConfiguration(
				appKey);
		if (xmppServerConfiguration == null) {
			return null;
		}
		XMPPConfiguration xmppConfiguration = new XMPPConfiguration(xmppServerConfiguration.getHostname(),
				xmppServerConfiguration.getPort(), xmppServerConfiguration.getConferenceService());
		return xmppConfiguration;
	}

	@GET
	@Path("/regcli")
	@Produces("application/json")
	public String register(@QueryParam("appKey") String appKey, @QueryParam("devId") String devId,
			@QueryParam("xmppAddress") String xmppAddress) {
		return RegisteredClientDAO.getInstance().registerClient(appKey, devId, xmppAddress).toString();
	}

	@GET
	@Path("/addr")
	@Produces("application/json")
	public String getXmppAddress(@QueryParam("appKey") String appKey, @QueryParam("devId") String devId) {
		return RegisteredClientDAO.getInstance().getClientXmppAddress(appKey, devId);
	}

	@GET
	@Path("/regto")
	@Produces("application/json")
	public String registerTopic(@QueryParam("appKey") String appKey, @QueryParam("topicName") String topicName,
			@QueryParam("roomName") String roomName) {
		return RegisteredTopicDAO.getInstance().registerTopic(appKey, topicName, roomName).toString();
	}

	@GET
	@Path("/unregto")
	@Produces("application/json")
	public void unregisterTopic(@QueryParam("appKey") String appKey, @QueryParam("topicName") String topicName) {
		// TODO Auto-generated method stub

	}

	@GET
	@Path("/room")
	@Produces("application/json")
	public String getRoomName(@QueryParam("appKey") String appKey, @QueryParam("topicName") String topicName) {
		return RegisteredTopicDAO.getInstance().getTopicRoomName(appKey, topicName);
	}

}
